
export const transformSelect = {
  nodeMap: {},
  parseData: function (target, config) {
    if (!target) return []
    const { showCode, props } = config
    return target.map(item => {
      let code = item['code']
      let label = item[props.label]
      let value = item[props.value]
      item['label'] = showCode ? `${code || value} ${label}` : label
      item['value'] = value
      transformSelect.nodeMap[value] = item
      return item
    })
  },
  parseBaseData: function (target, config) {
    if (!target) return []
    const { showCode, props } = config
    return target.map(item => {
      let code = item['code']
      let label = item[props.label] || item['name']
      let value = item[props.value] || item['code' || 'id']
      item['label'] = showCode ? `${code || value} ${label}` : label
      item['value'] = value
      transformSelect.nodeMap[value] = item
      return item
    })
  },
  transfData: function (target, config) {
    transformSelect.nodeMap = {}
    let tranfData = []
    if (config.type === 'base') {
      tranfData = transformSelect.parseBaseData(target, config)
    } else {
      tranfData = transformSelect.parseData(target, config)
    }
    return {
      nodeMap: transformSelect.nodeMap,
      tranfData: tranfData,
    }
  }
}

export const transformTreeSelect = {
  nodeMap: {},
  expandMap: {},
  parseData: function (target, config, targetCodes = []) {
    if (!target) return []
    const { showCode, props } = config
    return target.map(item => {
      let code = item['code']
      let label = item[props.label]
      let value = item[props.value]
      item['label'] = showCode ? `${code || value} ${label}` : label
      item['value'] = value
      targetCodes = [ ...targetCodes, value ]
      if (item.children && item.children.length > 0) {
        item.children = transformTreeSelect.parseData(item.children, config, targetCodes)
      }
      transformTreeSelect.nodeMap[value] = item
      transformTreeSelect.expandMap[value] = targetCodes
      targetCodes.pop()
      return item
    })
  },
  parseBaseData: function (target, config, targetCodes = []) {
    if (!target) return []
    const { showCode, props } = config
    return target.map(item => {
      let code = item['code']
      let label = item[props.label] || item['name']
      let value = item[props.value] || item['code' || 'id']
      item['label'] = showCode ? `${code || value} ${label}` : label
      item['value'] = value
      targetCodes = [ ...targetCodes, value ]
      if (item.children && item.children.length > 0) {
        item.children = transformTreeSelect.parseBaseData(item.children, config, targetCodes)
      }
      transformTreeSelect.nodeMap[value] = item
      transformTreeSelect.expandMap[value] = targetCodes
      targetCodes.pop()
      return item
    })
  },
  transfData: function (target, config) {
    transformTreeSelect.nodeMap = {}
    transformTreeSelect.expandMap = {}
    let tranfData = []
    if (config.type === 'base') {
      tranfData = transformTreeSelect.parseBaseData(target, config)
    } else {
      tranfData = transformTreeSelect.parseData(target, config)
    }
    return {
      nodeMap: transformTreeSelect.nodeMap,
      tranfData: tranfData,
      expandMap: transformTreeSelect.expandMap
    }
  }
}

